#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <string>
#include <iomanip>
#include <locale>
#include <sstream>
using namespace std;

int main(int argc, char **argv)
{
    string symbol[] = {"", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };


    for(string *p  = &symbol[0] ; p != &symbol[26]; ++p)
        for(string *q  = &symbol[0] ; q != &symbol[26]; ++q)
            for(string *r  = &symbol[0] ; r != &symbol[26]; ++r)
                for(string *s  = &symbol[0] ; s != &symbol[26]; ++s)
                {
                    string a = *p + *q + *r + *s;
                    string c = "curl -C \"tmp.csv\" \"http://download.finance.yahoo.com/d/quotes.csv?s="+ a + "&f=spjkm3m4rj6k5m8m6&gl=d&c=2008&ignore=.csv\" | tee output/"+
                        a+ ".csv";
//                    string c = "http://ichart.finance.yahoo.com/table.csv?d=6&e=1&f=2009&g=d&a=7&b=19&c=2004&ignore=.csv&s="+a;
                    const char *d = c.c_str();
                    system(d);


                    //Now make sense out of the data 
                    ifstream myfile;

                    a = "./output/"+a+".csv";
                    const char *c_file = a.c_str();
                    myfile.open(c_file);

                    // Runthis while there are elements in the file
                    while (myfile.good()) {
                        string line;
                        getline(myfile, line);

                        // Rotating position for the occurence of comma
                        size_t found, loop_found;
                        double result;

                        found = line.find_first_of(",");
                        string num = line.substr(0, found);

                        // Convert the string to a number

                        istringstream convert(num);
                        if (!(convert >> result)) {
                            result = 0;
                        }
                        else 
                            cout << result << endl;
                        while(found!=string::npos) {
                            loop_found = line.find_first_of(",", found + 1);
                            num = line.substr(found + 1, loop_found);
                            istringstream convert(num);
                            if (!(convert >> result)) {
                                result = 0;
                            }
                            else 
                                cout << result << endl;

                            found = loop_found;
                        }
                    }

                    // Close my file
                    myfile.close(); 


                }
}
